//
//  LAMerchantDetailBaseTableView.m
//  LYLazizDevelopDemo
//
//  Created by xbn_iMac on 2016/10/25.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LAMerchantDetailBaseTableView.h"
#import "UITableView+LZTableView.h"

@interface LAMerchantDetailBaseTableView ()<UIGestureRecognizerDelegate>

@end

@implementation LAMerchantDetailBaseTableView

-(instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
    self = [super initWithFrame:frame style:style];
    if (self) {
        self.isHaveLinkage = YES;
        [self configureRefresh];
//        [self createFooterView];
        self.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, 47, 0);
    }
    return self;
}

- (void)createFooterView {
    UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 47)];
    self.tableFooterView = v;
}

- (void)checkHaveData {}

#pragma mark - configure Refresh UI

- (void)configureRefresh{
    //    self.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(downLoadNewData)];
//    [self configHeaderRefreshText];
    
    if (![NSStringFromClass([self class]) isEqualToString:@"LAMerchantPhotoTableView"] && ![NSStringFromClass([self class]) isEqualToString:@"LAMerchantInformationTableView"]) {
        self.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(upLoadMoreData)];
        [self configFooterRefreshText];
    }
}

- (void)downLoadNewData {
}

- (void)upLoadMoreData {
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    NSLog(@"%@,%@",NSStringFromClass([self class]),NSStringFromSelector(_cmd));
}

// 是否添加联动效果
- (void) setIsHaveLinkage:(BOOL)isHaveLinkage {
    _isHaveLinkage = isHaveLinkage;
    if (isHaveLinkage) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(acceptMsg:) name:@"goTop" object:nil];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(acceptMsg:) name:@"leaveTop" object:nil];
    }
    else
    {
        [[NSNotificationCenter defaultCenter] removeObserver:self];
    }
}

-(void)acceptMsg : (NSNotification *)notification{
    
    if (!self.isHaveLinkage) {
        return;
    }
    
    NSString *notificationName = notification.name;
    
    if ([notificationName isEqualToString:@"goTop"]) {
        NSDictionary *userInfo = notification.userInfo;
        NSString *canScroll = userInfo[@"canScroll"];
        if ([canScroll isEqualToString:@"1"]) {
            self.canScroll = YES;
            self.showsVerticalScrollIndicator = YES;
        }
    }else if([notificationName isEqualToString:@"leaveTop"]){
        self.contentOffset = CGPointZero;
        self.canScroll = NO;
        self.showsVerticalScrollIndicator = NO;
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    [self lzScrollViewDidScroll:scrollView];
    
    if (!self.isHaveLinkage) {
        return;
    }
    
    if (!self.canScroll) {
        
        [scrollView setContentOffset:CGPointZero];
    }
    
    CGFloat offsetY = scrollView.contentOffset.y;
    if (offsetY<0) {
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"leaveTop" object:nil userInfo:@{@"canScroll":@"1"}];
    }
}

- (void)lzScrollViewDidScroll:(UIScrollView *)scrollView {
}

@end
